package model

import (
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"group_-no.2/utils"
	"group_-no.2/utils/errmsg"
	"mime/multipart"
	"time"
)

var AccessKey = utils.AccessKey
var SecretKey = utils.SecretKey
var Bucket = utils.Bucket
var Endpoint = utils.EndPoint

func UploadFile(file multipart.File, header *multipart.FileHeader) (string, int) {
	client, err := oss.New(Endpoint, AccessKey, SecretKey)
	if err != nil {
		return "", errmsg.ERROR
	}

	bucket, err := client.Bucket(Bucket)
	if err != nil {
		return "", errmsg.ERROR
	}

	filename := fmt.Sprintf("%d_%s", time.Now().Unix(), header.Filename)

	err = bucket.PutObject(filename, file)
	if err != nil {
		return "", errmsg.ERROR
	}

	url := fmt.Sprintf("https://%s.%s/%s", Bucket, Endpoint, filename)

	return url, errmsg.SUCCESS
}
